/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.dw.lidejia.nbthrift;

import com.snda.dw.lidejia.thriftgen.UserStorage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author lidejia
 */
public class Client {

    private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int threadNum = Integer.parseInt(args[0]);
        ExecutorService es = Executors.newCachedThreadPool();
        for (int i = 0; i < threadNum; i++) {
            es.execute(new Runnable() {

                @Override
                public void run() {
                    while (true) {
                        try {
                            //
                            final TSocket socket = new TSocket("127.0.0.1", 8888);
                            socket.setTimeout(5000);
                            //
                            final TTransport transport = socket;
                            final TProtocol protocol = new TBinaryProtocol(transport);
                            transport.open();
                            try {
                                final UserStorage.Client client = new UserStorage.Client(protocol);
                                LOGGER.info(client.retrieve(1).getName());
                            } finally {
                                transport.close();
                            }
                        } catch (TException ex) {
                            LOGGER.error("Thrift Error", ex);
                        }
                    }
                }
            });
        }
        es.shutdown();
    }
}
